package test;

import gui.ScenarioDialog;
import houseHold.House;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import locations.LocationArchive;

import action.ActionPool;

import done.DBConnection;

import person.Location;
import person.PersonAchive;
import reader.ScenarioBuilder;
import simulation.ScheduleProgram;
import simulation.SimRunner;
import timer.Time;
import timer.Timer;
import toXML.LoadFamilyXML;
import toXML.LoadHouseXML;

public class test {
	public static void main(String args[]) {

		File fam = new File("somefamily2.xml");
		File house = new File("somehouse2.xml");
		LoadFamilyXML.loadXML(fam);
		LoadHouseXML.loadXML(house);
		//System.out.println(House.getInstance());
		File s1 = new File("Scenario/Alice2.sce");
		File s2=new File("Scenario/Bob2.sce");
		LocationArchive.init();
		ActionPool.clear();
		Vector<String> data=new Vector<String>();
		data.add(read(s1));
		data.add(read(s2));
		for(int i=0;i<data.size();i++){
			ScenarioBuilder sb=new ScenarioBuilder();
			sb.build(data.get(i));
		}
		DBConnection.initConnection();
		DBConnection.CreateTables();
		DBConnection.insertMainData();
		

		for(int i=0;i<Time.dates.size();i++)
		{
			SimRunner.runSimulation(Time.dates.get(i));
			while(ScheduleProgram.masterFinish()!=true)
			{
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("SIMULATION "+i+ " DONE");
			try {
				Thread.sleep(2500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}//add date here
	}
	
	public static String read(File f)
	{
		String result=new String(),nextLine=new String();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(f));
			while ((nextLine = reader.readLine()) != null)
				result=result.concat(nextLine).concat("\n");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
